//
//  MCViewController.swift
//  WisdomSpace
//
//  Created by goulela on 2017/8/29.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit
import SnapKit

/**
 所有视图控制器的父类
 */
class MCViewController: UIViewController {

    
    /**
     * 是否隐藏导航栏上的那条黑线
     */
    public var navigationBarLineHidden = false {
        didSet{
            navBarHairLine = MCFindNavigationBarLine(view: (navigationController?.navigationBar))
        }
    }
    
    
    /**
     * 当前页面更换navigationBar的背景图片
     */
    public var navigationBarBackgroundImage : UIImage?

    /**
     * 当前控制器的title文字颜色
     */
    public var navigationBarTitleColor : UIColor?

    
    
    private var navBarHairLine : UIImageView?
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        let bar = self.navigationController?.navigationBar

        if navigationBarBackgroundImage != nil {
            bar?.setBackgroundImage(navigationBarBackgroundImage, for: UIBarMetrics.default)
        }
        
        if navigationBarTitleColor != nil {
            bar?.titleTextAttributes = [NSAttributedStringKey.font : MCFont(19.0),NSAttributedStringKey.foregroundColor : navigationBarTitleColor!]
        }

        
        navBarHairLine?.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navBarHairLine?.isHidden = false
        
        let bar = self.navigationController?.navigationBar
        bar?.setBackgroundImage(UIImage.init(named: "Nav_background"), for: UIBarMetrics.default)
        
        bar?.titleTextAttributes = [NSAttributedStringKey.font : MCFont(19.0),NSAttributedStringKey.foregroundColor : UIColor.black]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        view.backgroundColor = UIColor.white
        
        
        navigationItem.leftBarButtonItem = MCSetBarButtonItem_image(#imageLiteral(resourceName: "Reuse_back"), target: self, selector: #selector(leftItemClicked), isLeft: true)
    }
    
    
    @objc func leftItemClicked() {
        self.navigationController?.popViewController(animated: true)
    }
}
